package ua.edu.nuos.jeetraining2012.cms.entity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

/**
 * HomeWorkEntity.
 * <p/>
 * Class that store home works for course materials.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 18.05.13 Time: 16:27
 * @since 1.0
 */
@Entity
@Table(name = "homeworks")
public class HomeWorkEntity implements Serializable {

    /**
     * Id.
     */
    @Id
    @SequenceGenerator(
            name = "homeworks_id_seq",
            sequenceName = "homeworks_id_seq",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "homeworks_id_seq"
    )
    @Column(
            name = "id",
            nullable = false,
            insertable = true,
            updatable = false,
            length = 11,
            precision = 0,
            columnDefinition = "SERIAL"
    )
    @NotNull
    private long id;


    /**
     * HomeWork title.
     * <p/>
     * The title of the homework.
     */
    @Column(
            name = "title",
            nullable = false,
            insertable = true,
            updatable = true,
            length = 256
    )
    @Basic(optional = false)
    @NotNull
    @Size(max = 256)
    private String title;


    /**
     * HomeWork Description.
     * E.g. maximum info about, what should be done by the student to success pass this hw.
     */
    @Column(name = "description", nullable = false)
    @Basic(optional = false)
    @NotNull
    private String description;

    /**
     * Additional files, that can be attached to this HW.
     */
    @Column(name = "files", nullable = true)
    @Basic(optional = true)
    private String filesURL;

    /**
     * Begin of the work.
     */
    @Column(name = "start_date", nullable = false)
    @Basic(optional = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    @NotNull
    private Date startDate;

    /**
     * Deadline.
     */
    @Column(name = "due_date", nullable = false)
    @Basic(optional = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    @NotNull
    private Date dueDate;

    /**
     * Scores, that student can receive for well-done job.
     */
    @Column(name = "scores", nullable = false)
    @Basic(optional = false)
    @NotNull
    private int scores;

    /** Material ID. */
    @ManyToOne
    private CourseMaterialEntity material;

    // Default constructor
    public HomeWorkEntity() {}

    // Getters & Setters

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getFilesURL() {
        return filesURL;
    }

    public void setFilesURL(String filesURL) {
        this.filesURL = filesURL;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public int getScores() {
        return scores;
    }

    public void setScores(int scores) {
        this.scores = scores;
    }

    public CourseMaterialEntity getMaterial() {
        return material;
    }

    public void setMaterial(CourseMaterialEntity material) {
        this.material = material;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    // Overrides

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        HomeWorkEntity that = (HomeWorkEntity) o;

        if (id != that.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }

    @Override
    public String toString() {
        return "HomeWorkEntity{" +
                "id=" + id +
                ", title='" + title + "'" +
                ", description='" + description + '\'' +
                ", filesURL='" + filesURL + '\'' +
                ", startDate=" + startDate +
                ", dueDate=" + dueDate +
                ", scores=" + scores +
                ", material=" + material.getId() +
                '}';
    }
}
